package com.service;

import com.entity.Tag;
import com.mapper.TagMapper;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class TagService implements TagMapper {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 查询全部标签
     * @return 返回全部标签集合
     */
    @Override
    public List<Tag> findAllTag() {
        SqlSession sqlSession = factory.openSession();
        TagMapper mapper = sqlSession.getMapper(TagMapper.class);
        List<Tag> AllTag = mapper.findAllTag();
        return AllTag;
    }

    /**
     * 根据标签名查询标签
     * @param tag_name
     * @return 返回单个标签对象
     */
    @Override
    public Tag findTagByName(String tag_name) {
        SqlSession sqlSession = factory.openSession();
        TagMapper mapper = sqlSession.getMapper(TagMapper.class);
        Tag tag = mapper.findTagByName(tag_name);
        return tag;

    }

    /**
     * 添加标签
     * @param tagName
     * @return 返回新建标签对象
     */
    @Override
    public int addTag(String tagName) {
        SqlSession sqlSession = factory.openSession();
        TagMapper mapper = sqlSession.getMapper(TagMapper.class);
        int rows = mapper.addTag(tagName);
        System.out.println(true);
        sqlSession.commit();
        sqlSession.close();
        return rows;
    }

    /**
     * 根据标签名删除该标签
     * @param name
     * @return 删除成功返回true ，失败返回false
     */
    @Override
    public boolean delTagByName(String name) {
        isTagExist(name);
        SqlSession sqlSession = factory.openSession();
        TagMapper mapper = sqlSession.getMapper(TagMapper.class);
        boolean isDel = mapper.delTagByName(name);
        sqlSession.commit();
        return isDel;
    }

    @Override
    public int updateTagByName(String oldTag, String newTag) {
        SqlSession sqlSession = factory.openSession();
        TagMapper mapper = sqlSession.getMapper(TagMapper.class);
        int rows = mapper.updateTagByName(oldTag, newTag);
        sqlSession.commit();
        return rows;
    }


    /**
     * 判断标签是否存在并且新建标签
     * @param name 标签名
     * @return 标签存在返回 false，不存在则新建并且返回true
     */
    public boolean isTagExist(String name){
        TagService tagService = new TagService();
        try {
            Object object= tagService.findTagByName(name).getTag_name();
            System.out.println(object+"标签已存在，新建失败");
            return false;//
        }catch (Exception e){
            addTag(name);
            System.out.println("标签不存在，已新建标签");
            return true;//标签已存在，新建失败
        }
    }


    public static void main(String[] args) {

        TagService tagService = new TagService();
        boolean tagExist = tagService.isTagExist("php4");
        System.out.println(tagExist);

//        String name="php";
//        try {
//            Object object= tagService.findTagByName(name).getTag_id();
//            System.out.println(object);
//        }catch (Exception e){
//            System.out.println("标签不存在");
//        }

    }
}
